//the global fields
var xml_request;

var dataTable;
var dataTableBody;
var tbodyEidt;
var countTotal = 0;
var count = 0;
var skillNameGlobal;
var skillGlobal;
var dropdownListTag;
var skillBranchId;
var isOK;


/**
 * @Created Date:03/03/2010
 * @Functionality:  get the XMLHttpRequest Object
 * @Modified Date:
 * @Modified Person:
 * **/
function getXMLHttpRequest() {

	if(window.XMLHttpRequest){
		xml_request=new XMLHttpRequest();
	}
	// pour les navigateur: IE6,IE5.5,IE5
	else if(window.ActiveXObject){
		xml_request= new ActiveXObject("Microsoft.XMLHTTP");
		if(!xml_request){
			xml_request = new ActiveXObject("Msxml2.XMLHTTP");
		}
	}
} 

/**
* @Created Date:03/03/2010
* @Functionality: send request function
* @Modified Date:
	* @Modified Person:
		* **/
		function sendRequestView() {  
	var year = document.getElementById("year").value;

	var gradeName = document.getElementById("gradeName").value;

	var msgError = document.getElementById("msgError");

	msgError.innerHTML = "Please fill in mandatory fields  ";
	msgError.className = "error";

	if(gradeName==""){
		msgError.innerHTML = msgError.innerHTML+"\"grade\"";
	}

	if(year==""){
		msgError.innerHTML = msgError.innerHTML+"  \"year\"";
	}

	if(gradeName!=""&&year!=""){

		msgError.innerHTML = "";
		msgError.className = "";
		$("#editSkill").hide();

		var url = "viewGradeSkill.do?action=all&year="+year+"&gradeName="+gradeName;
		getXMLHttpRequest();   
		xml_request.open("GET", url, false);   
		xml_request.onreadystatechange = completeView;//give response function
		xml_request.send(null);  // send request
	}
} 

/**
* @Created Date:03/03/2010
* @Functionality: finished the view skill
* @Modified Date:
	* @Modified Person:
		* **/
		function completeView(){
	if (xml_request.readyState == 4) { 
		if (xml_request.status == 200) { 

			dataTable = document.getElementById("viewSkill");
			dataTableBody = document.getElementById("viewSkillBody");

			clearData();

			var xmlDoc =xml_request.responseXML.documentElement;
			var xsel = xmlDoc.getElementsByTagName("skillBranches");

			var countSkill = xmlDoc.getElementsByTagName("countSkill")[0].firstChild.data;

			var countSkillInt = parseInt(countSkill);

			var skillName;
			var pass;
			var entry;
			var middle;
			var hight;
			var countSkillBranch;
			var countSkillBranchInt;

			var skillBrancheName;
			var no;
			var duration;
			var expected;

			for(var i=0;i<countSkillInt;i++){
				try{
					skillName=xmlDoc.getElementsByTagName("skillName")[i].firstChild.data;
				}catch (e){skillName='';}
				try{
					pass=xmlDoc.getElementsByTagName("pass")[i].firstChild.data;
				}catch (e){pass='';}
				try{
					entry=xmlDoc.getElementsByTagName("entry")[i].firstChild.data;
				}catch (e){entry='';}
				try{
					middle=xmlDoc.getElementsByTagName("middle")[i].firstChild.data;
				}catch (e){middle='';}
				try{
					hight=xmlDoc.getElementsByTagName("hight")[i].firstChild.data;
				}catch (e){hight='';}

				var row;
				var col;
				var dataTableView;
				var dataTableBodyView;
				var trView1;
				var tdView1;
				var trView2;
				var tdView2;
				var fieldset;

				row=document.createElement("tr");
				col = document.createElement("td");

				fieldset = document.createElement("fieldset");
				var legend = document.createElement("legend");
				legend.innerHTML =skillName; 
				fieldset.appendChild(legend);

				dataTableView = document.createElement("table");
				dataTableBodyView = document.createElement("tbody");
				trView1 = document.createElement("tr"); 
				tdView1 = document.createElement("td");
				trView2 = document.createElement("tr"); 
				tdView2 = document.createElement("td");
				fieldset.appendChild(createSkillTitle(skillName,pass,entry,middle,hight));
				tdView1.appendChild(fieldset);
				trView1.appendChild(tdView1);
				dataTableBodyView.appendChild(trView1); 
				dataTableView.appendChild(dataTableBodyView);
				col.appendChild(dataTableView);
				row.appendChild(col);
				dataTableBody.appendChild(row);

				try{
					countSkillBranch=xmlDoc.getElementsByTagName("countSkillBranch")[i].firstChild.data;
				}catch (e){countSkillBranch=0;}

				countSkillBranchInt = parseInt(countSkillBranch);

				var SkillBranchView = document.createElement("table");
				var SkillBranchBodyView = document.createElement("tbody"); 
				SkillBranchView.className = "blueTable span-17";

				SkillBranchBodyView.appendChild(createTh());


				for(var j=0;j<countSkillBranchInt;j++){
					var skillBrancheNameNode=xsel[i].childNodes[j+1];

					skillBrancheName = skillBrancheNameNode.childNodes[0].firstChild.nodeValue;
					no = skillBrancheNameNode.childNodes[1].firstChild.nodeValue;
					duration = skillBrancheNameNode.childNodes[2].firstChild.nodeValue;
					expected = skillBrancheNameNode.childNodes[3].firstChild.nodeValue;

					SkillBranchBodyView.appendChild(createRow(skillBrancheName,no,duration,expected));

				}
				SkillBranchView.appendChild(SkillBranchBodyView);
				fieldset.appendChild(SkillBranchView);
				tdView2.appendChild(fieldset);
				trView2.appendChild(tdView2);
				dataTableBodyView.appendChild(trView2); 
				dataTableView.appendChild(dataTableBodyView);
				col.appendChild(dataTableView);
				row.appendChild(col);

			}
		}
	}
	gridColor();
}

/**
 * @Created Date:03/03/2010
 * @Functionality:  create one row for the table, this row has skill, skillBranch,No.of Question and duration 
 * @Modified Date:
 * @Modified Person:
 * **/
function createTh(){
	//create tr
	var row=document.createElement("tr");
	//create td  
	var cell=document.createElement("th"); 
	cell.setAttribute("width", "25%");
	var celldata=document.createTextNode("Skill Branch");
	cell.appendChild(celldata);
	row.appendChild(cell);

	var cell1=document.createElement("th");
	cell1.setAttribute("width", "25%");
	var celldata1=document.createTextNode("No. of Q's");
	cell1.appendChild(celldata1);
	row.appendChild(cell1);       

	var cell2=document.createElement("th");
	cell2.setAttribute("width", "25%");
	var celldata2=document.createTextNode("Duration(Min)");
	cell2.appendChild(celldata2);
	row.appendChild(cell2);

	var cell3=document.createElement("th");
	cell3.setAttribute("width", "25%");
	var celldata3=document.createTextNode("Expect(%)");
	cell3.appendChild(celldata3);
	row.appendChild(cell3);

	return row;
} 

/**
* @Created Date:03/03/2010
* @Functionality:  create one row for the table, this row has skill, skillBranch,No.of Question and duration 
* @Modified Date:
	* @Modified Person:
		* **/
		function createRow(skillBrancheName,no,duration,expected){
	//create tr
	var row=document.createElement("tr");
	//create td  
	var cell=document.createElement("td"); 
	cell.setAttribute("width", "25%");
	var celldata=document.createTextNode(skillBrancheName);
	cell.appendChild(celldata);
	row.appendChild(cell);

	var cell1=document.createElement("td");
	cell1.setAttribute("width", "25%");
	var celldata1=document.createTextNode(no);
	cell1.appendChild(celldata1);
	row.appendChild(cell1);       

	var cell2=document.createElement("td");
	cell2.setAttribute("width", "25%");
	var celldata2=document.createTextNode(duration);
	cell2.appendChild(celldata2);
	row.appendChild(cell2);

	var cell3=document.createElement("td");
	cell3.setAttribute("width", "25%");
	var celldata3=document.createTextNode(expected);
	cell3.appendChild(celldata3);
	row.appendChild(cell3);

	return row;
}



function createSkillTitle(skillName,pass,entry,middle,hight){
	//row=document.createElement("tr");
	//var rowtd=document.createElement("td");

	var tabel = document.createElement("table");
	var tabelBody = document.createElement("tbody");
	var tr1 = document.createElement("tr");

	var cell1=document.createElement("td"); 
	cell1.setAttribute("width", "25%");
	var celldata1=document.createTextNode("Pass(%):  "+pass);
	cell1.appendChild(celldata1);
	tr1.appendChild(cell1);

	var cell2=document.createElement("td");  
	cell2.setAttribute("width", "25%");
	var celldata2=document.createTextNode("Entry(%):  "+entry);
	cell2.appendChild(celldata2);
	tr1.appendChild(cell2);

	var cell3=document.createElement("td");  
	cell3.setAttribute("width", "25%");
	var celldata3=document.createTextNode("Middle(%):  "+middle);
	cell3.appendChild(celldata3);
	tr1.appendChild(cell3);

	var cell4=document.createElement("td");
	cell4.setAttribute("width", "25%");
	var celldata4=document.createTextNode("High(%):  "+hight);
	cell4.appendChild(celldata4);
	tr1.appendChild(cell4);

	tabelBody.appendChild(tr1);
	tabel.appendChild(tabelBody);

	return tabel;
}

function clearData(){

	dataTable.removeChild(dataTable.childNodes[0]); 
	dataTableBody = createTHead(); 
}

function createTHead(){

	var tbody = document.createElement("tbody");
	tbody.setAttribute("id","viewSkillBody");
	dataTable.appendChild(tbody);

	return tbody;
}

function addSkill(){

	var year = document.getElementById("year").value;

	var gradeName = document.getElementById("gradeName").value;

	skillGlobal = document.getElementById("skill");

	var msgError = document.getElementById("msgError");

	msgError.innerHTML = "Please fill in mandatory fields  ";
	msgError.className = "error";

	if(gradeName==""){
		msgError.innerHTML = msgError.innerHTML+"\"grade\"";
	}

	if(year==""){
		msgError.innerHTML = msgError.innerHTML+"  \"year\"";
	}

	if(skillGlobal.value == ""){
		msgError.innerHTML = msgError.innerHTML+"  \"skill\"";
	}

	if(gradeName!=""&&year!=""&&skillGlobal.value != ""){

		msgError.innerHTML = "";
		msgError.className = "";

		dataTable = document.getElementById("viewSkill");
		dataTableBody = document.getElementById("viewSkillBody");

		clearData();
		resetEditTableTitle('','','','');

		$("#editSkill").show();

		tbodyEidt = document.getElementById("listAllSkillBranches");

		countTotal = countTotal+1;
		for(var i=1;i<countTotal;i++){
			tbodyEidt.removeChild(tbodyEidt.childNodes[1]); 
		}

		countTotal = 0;
		count = 0;

		skillNameGlobal = skillGlobal.options[skillGlobal.selectedIndex].innerHTML;

		var skillTitle = document.getElementById("skillTitle");
		skillTitle.innerHTML = skillNameGlobal;

		var url = "viewGradeSkill.do?action=single&skill="+skillGlobal.value+"&year="+year+"&gradeName="+gradeName;
		getXMLHttpRequest();   
		xml_request.open("GET", url, false);   
		xml_request.onreadystatechange = completeAddSkill;//give response function
		xml_request.send(null);  // send request
	}

}

function addSkillBranchEdit(){

	var countElement = document.getElementById("count");
	countElement.value = count;

	var save = document.getElementById("save");

	setSave();
}

function completeAddSkill(){
	if (xml_request.readyState == 4) { 
		if (xml_request.status == 200) {

			var xmlDoc =xml_request.responseXML.documentElement;
			var xsel = xmlDoc.getElementsByTagName("skillBranches");

			var skillName;
			var pass;
			var entry;
			var middle;
			var hight;
			var countSkillBranch;
			var countSkillBranchInt;

			var skillBrancheName;
			var no;
			var duration;
			var expected;


			try{
				skillName=xmlDoc.getElementsByTagName("skillName")[0].firstChild.data;
			}catch (e){skillName='';}
			try{
				pass=xmlDoc.getElementsByTagName("pass")[0].firstChild.data;
			}catch (e){pass='';}
			try{
				entry=xmlDoc.getElementsByTagName("entry")[0].firstChild.data;
			}catch (e){entry='';}
			try{
				middle=xmlDoc.getElementsByTagName("middle")[0].firstChild.data;
			}catch (e){middle='';}
			try{
				hight=xmlDoc.getElementsByTagName("hight")[0].firstChild.data;
			}catch (e){hight='';}

			try{
				countSkillBranch=xmlDoc.getElementsByTagName("countSkillBranch")[0].firstChild.data;
			}catch (e){countSkillBranch=0;}

			resetEditTableTitle(pass,entry,middle,hight);

			countSkillBranchInt = parseInt(countSkillBranch);

			for(var j=0;j<countSkillBranchInt;j++){
				var skillBrancheNameNode=xsel[0].childNodes[j+1];

				skillBrancheName = skillBrancheNameNode.childNodes[0].firstChild.nodeValue;
				no = skillBrancheNameNode.childNodes[1].firstChild.nodeValue;
				duration = skillBrancheNameNode.childNodes[2].firstChild.nodeValue;
				expected = skillBrancheNameNode.childNodes[3].firstChild.nodeValue;
				skillBranchId = skillBrancheNameNode.childNodes[4].firstChild.nodeValue;		
				tbodyEidt.appendChild(createRowEditTable(no,duration,expected));

				dropdownListTag = "selSkillBranch"+count;
				skillBranchList();	
				count++;
				countTotal++;

			}

		}
	}


	gridColor();
}

function resetEditTableTitle(pass,entry,middle,hight){
	var passEdit = document.getElementById("pass");
	passEdit.value = pass;

	var entryEdit = document.getElementById("entryScore");
	entryEdit.value = entry;

	var middleEdit = document.getElementById("middleScore");
	middleEdit.value = middle;

	var hightEdit = document.getElementById("highScore");
	hightEdit.value = hight;
}


function createRowEditTable(no,duration,expected){
	//create tr
	var row=document.createElement("tr");
	row.setAttribute("id","tr"+count);
	//create td  
	var cell=document.createElement("td"); 
	cell.setAttribute("width", "25%");
	var celldata=document.createElement("select");
	celldata.setAttribute("name","skillBranch"+count);
	celldata.setAttribute("id", "selSkillBranch"+count);
	cell.appendChild(celldata);
	row.appendChild(cell);

	var cell1=document.createElement("td");
	cell1.setAttribute("width", "25%");
	var celldata1=document.createElement("input");
	celldata1.setAttribute("type", "text");
	celldata1.setAttribute("width", "50");
	celldata1.setAttribute("name","noQuestion"+count);
	celldata1.setAttribute("value",no);
	celldata1.onkeyup=function(){onlyNum(this)};
	cell1.appendChild(celldata1);
	row.appendChild(cell1);       

	var cell2=document.createElement("td");
	cell2.setAttribute("width", "25%");
	var celldata2=document.createElement("input");
	celldata2.setAttribute("type", "text");
	celldata2.setAttribute("width", "50");
	celldata2.setAttribute("name","duration"+count);
	celldata2.setAttribute("value",duration);
	celldata2.onkeyup=function(){onlyNum(this)};
	cell2.appendChild(celldata2);
	row.appendChild(cell2);

	var cell3=document.createElement("td");
	cell3.setAttribute("width", "25%");
	var celldata3=document.createElement("input");
	celldata3.setAttribute("type", "text");
	celldata3.setAttribute("width", "50");
	celldata3.setAttribute("name","expect"+count);
	celldata3.setAttribute("value",expected);
	celldata3.onkeyup=function(){onlyNum(this)};
	cell3.appendChild(celldata3);
	row.appendChild(cell3);


	var cell4=document.createElement("td");
	var inpu = document.createElement("Input");
	inpu.setAttribute("type","button");
	inpu.setAttribute("width","60");
	inpu.setAttribute("height","25");
	inpu.setAttribute("value","Delete");
	inpu.setAttribute("id","del"+count);
	inpu.onclick=function(){deleteRowEdit(this)};

	cell4.appendChild(inpu);
	row.appendChild(cell4);

	return row;
}

function deleteRowEdit(element){

	var idDel = (element.id).substr(3,(element.id).lengh);

	var trdel = document.getElementById("tr"+idDel);

	tbodyEidt.removeChild(trdel);

	countTotal--;

	setSave();
}

function addLine(){

	tbodyEidt.appendChild(createRowEditTable('','',''));

	dropdownListTag = "selSkillBranch"+count;

	skillBranchId = "";

	skillBranchList();

	countTotal++;
	count++;

	var countElement = document.getElementById("count");
	countElement.value = count;

	setSave();

	gridColor();
}

function onlyNum(o)   
{   
	o.value=o.value.replace(/[^\d]/g,''); 

	if(o.value == 0){
		o.value = "";
	}
}

function skillBranchList(){
	var url = "cascadRelatedSkillBranch.do?idValue="+skillGlobal.value;
	getXMLHttpRequest();   
	xml_request.open("GET", url, false);   
	xml_request.onreadystatechange = completeSkillBranchList;//give response function
	xml_request.send(null);  // send request
}

function completeSkillBranchList() {   
	if (xml_request.readyState == 4) { //  check object status   
		if (xml_request.status == 200) { // handle the information if response successful   
			updateMenuEdit();   
		} else { //page is exception 
			alert("your request has exception");   
		}   
	}   
}   

/**
* @Created Date:03/02/2010
* @Functionality: function of updating menu 
* @Modified Date:
	* @Modified Person:
		* **/

		function updateMenuEdit() {   

	var xmlDoc =xml_request.responseXML.documentElement;
	var xsel = xmlDoc.getElementsByTagName("select");
	var subRoot = document.getElementById(dropdownListTag);
	subRoot.options.length=0;
	for(var i=0;i<xsel.length;i++){

		try{
			var xValue=xsel[i].childNodes[0].firstChild.nodeValue;
			var xText=xsel[i].childNodes[1].firstChild.nodeValue;
			var option=new Option(xText,xValue);        
			subRoot[i]=option;

		}catch(e){
			xValue = "";
			var xText=xsel[i].childNodes[1].firstChild.nodeValue;
			var option=new Option(xText,xValue);

			subRoot[i]=option;

		}
	} 


	subRoot.value = skillBranchId;
}   

function check(){

	isOK = true;

	var grad = document.getElementById("gradeName").value;

	var year = document.getElementById("year").value



}

function setSave(){
	var save = document.getElementById("save");

	if(countTotal>0){
		save.disabled = false;
	}else{
		save.disabled = true;
	}
}

function resetAll(){
	var rang = document.getElementById("rang");
	
	rang.innerHTML = "";
}

function deleteSkill(){
	
	var year = document.getElementById("year").value;

	var gradeName = document.getElementById("gradeName").value;
	
	var url ="setGrade.do?action=delete&skill="+skillGlobal.value+"&gradeName="+gradeName+"&year="+year;
	
	getXMLHttpRequest();   
	xml_request.open("GET", url, false);   
	xml_request.onreadystatechange = completeDeleteSkill;//give response function
	xml_request.send(null);
}

function completeDeleteSkill(){
	
	if (xml_request.readyState == 4) { //  check object status   
		if (xml_request.status == 200) { // handle the information if response successful   
			msgError.innerHTML = "";
			msgError.className = "";
			$("#editSkill").hide();  
		} else { //page is exception 
			alert("your request has exception");   
		}   
	}   
	
}
